package com.weixin.weixinmanage.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.weixin.weixinmanage.entity.Course;
import com.weixin.weixinmanage.service.CourseService;

public class CourseAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = -407938911442900803L;
	
	private Logger log = Logger.getLogger(CourseAction.class);
	
	private CourseService courseService;
	
	private Course course;
	
	private HashMap<String, Object> queryJson;
	
	public String getAllData(){
		queryJson = new HashMap<String, Object>();
		try{
			if(course!=null){
				course.setCid(null);
			}
			List<Course> couresList = courseService.select(course);
			queryJson.put("result", couresList);
		}catch(Exception e){
			log.error("", e);
		}
		return "getAllData";
	}
	
	public String toUpdate(){
		try{
			course = courseService.selectById(course.getCid());
		}catch(Exception e){
			log.error("", e);
		}
		return "toUpdate";
	}
	
	public String update() throws IOException{
		try{
			courseService.update(course);
		}catch(Exception e){
			log.error("", e);
		}
		String html = "<script language='javascript'>parent.window.closeOver();</script>";
		HttpServletResponse response = ServletActionContext.getResponse(); 
		response.setContentType("text/html;charset=utf-8"); 
	    response.setCharacterEncoding("UTF-8");
	    response.setHeader("Cache-Control", "no-cache"); 
	    PrintWriter out = response.getWriter(); 
		out.println(html);
	    out.flush();
	    out.close();
	    course.setCid(null);
		return null;
	}
	
	public String del(){
		queryJson = new HashMap<String, Object>();
		try{
			courseService.update(course);
			queryJson.put("message", "success");
		}catch(Exception e){
			queryJson.put("message", "fail");
		}
		course.setCid(null);
		return "del";
	}
	
	public String publish(){
		queryJson = new HashMap<String, Object>();
		try{
			courseService.update(course);
			queryJson.put("message", "success");
		}catch(Exception e){
			queryJson.put("message", "fail");
		}
		course.setCid(null);
		return "publish";
	}
	
	public String regain(){
		queryJson = new HashMap<String, Object>();
		try{
			courseService.update(course);
			queryJson.put("message", "success");
		}catch(Exception e){
			queryJson.put("message", "fail");
		}
		course.setCid(null);
		return "regain";
	}
	
	public String huishou(){
		queryJson = new HashMap<String, Object>();
		try{
			courseService.update(course);
			queryJson.put("message", "success");
		}catch(Exception e){
			queryJson.put("message", "fail");
		}
		course.setCid(null);
		return "huishou";
	}
	
	public CourseService getCourseService() {
		return courseService;
	}

	public void setCourseService(CourseService courseService) {
		this.courseService = courseService;
	}

	public Course getCourse() {
		return course;
	}

	public void setCourse(Course course) {
		this.course = course;
	}

	public HashMap<String, Object> getQueryJson() {
		return queryJson;
	}

	public void setQueryJson(HashMap<String, Object> queryJson) {
		this.queryJson = queryJson;
	}

}
